// Package service internal/service/department_service.go
package service

import (
	"errors"
	"github.com/gin-gonic/gin"
	"github.com/go-playground/validator/v10"
	"gozrd-admin-api/internal/entity/dto"
	"gozrd-admin-api/internal/entity/model"
	"gozrd-admin-api/internal/entity/vo"
	"gozrd-admin-api/internal/repository"
	"gozrd-admin-api/pkg/response"
	"time"
)

// IDepartmentService 部门服务接口
type IDepartmentService interface {
	GetSysDeptList(c *gin.Context, deptName, deptStatus string)
	CreateSysDept(c *gin.Context, sysDept model.SysDept)
	GetSysDeptById(c *gin.Context, id int)
	UpdateSysDept(c *gin.Context, dept model.SysDept)
	DeleteSysDeptById(c *gin.Context, dto dto.SysDeptIdDto)
	QuerySysDeptVoList(c *gin.Context)
}

// DepartmentServiceImpl 部门服务实现
type DepartmentServiceImpl struct {
	deptRepository repository.IDepartmentRepository
	validator      *validator.Validate
}

// NewDepartmentService 创建部门服务实例
func NewDepartmentService(deptRepo repository.IDepartmentRepository) IDepartmentService {
	return &DepartmentServiceImpl{
		deptRepository: deptRepo,
		validator:      validator.New(),
	}
}

// 部门相关错误
var (
	DeptExist = errors.New("部门名称已存在")
	DeptInUse = errors.New("部门正在被用户使用，无法删除")
)

// GetSysDeptList 获取部门列表
func (d *DepartmentServiceImpl) GetSysDeptList(c *gin.Context, deptName, deptStatus string) {
	// 调用Repository层
	depts, err := d.deptRepository.GetSysDeptList(deptName, deptStatus)
	if err != nil {
		response.Fail(c, response.ErrDatabase, "获取部门列表失败："+err.Error())
		return
	}

	response.Success(c, depts)
}

// CreateSysDept 创建部门
func (d *DepartmentServiceImpl) CreateSysDept(c *gin.Context, dept model.SysDept) {
	// 验证模型
	if err := d.validator.Struct(dept); err != nil {
		response.Fail(c, response.ErrParams, err.Error())
		return
	}

	// 补充领域模型中的其他字段
	dept.CreateTime = time.Now()

	// 调用Repository
	err := d.deptRepository.CreateSysDept(dept)
	if err != nil {
		if err == repository.DeptExist {
			response.Fail(c, response.BadRequest, "部门名称已存在")
			return
		}
		response.Fail(c, response.ErrDatabase, "创建部门失败："+err.Error())
		return
	}

	response.SuccessWithMessage(c, "ok", nil)
}

// GetSysDeptById 通过id获取部门
func (d *DepartmentServiceImpl) GetSysDeptById(c *gin.Context, id int) {
	dept, err := d.deptRepository.GetSysDeptById(id)
	if err != nil {
		response.Fail(c, response.ErrDatabase, "通过id获取部门错误："+err.Error())
		return
	}
	response.Success(c, dept)
}

// UpdateSysDept 修改部门
func (d *DepartmentServiceImpl) UpdateSysDept(c *gin.Context, dept model.SysDept) {
	// 验证模型
	if err := d.validator.Struct(dept); err != nil {
		response.Fail(c, response.ErrParams, err.Error())
		return
	}

	// 调用Repository层
	updatedDept, err := d.deptRepository.UpdateSysDept(dept)
	if err != nil {
		response.Fail(c, response.ErrDatabase, "更新部门失败："+err.Error())
		return
	}

	response.Success(c, updatedDept)
}

// DeleteSysDeptById 删除部门
func (d *DepartmentServiceImpl) DeleteSysDeptById(c *gin.Context, dto dto.SysDeptIdDto) {
	// 验证DTO
	if err := d.validator.Struct(dto); err != nil {
		response.Fail(c, response.ErrParams, err.Error())
		return
	}

	// 调用Repository层
	err := d.deptRepository.DeleteSysDeptById(dto.Id)
	if err != nil {
		if err == repository.DeptInUse {
			response.Fail(c, response.BadRequest, "部门正在被用户使用，无法删除")
			return
		}
		response.Fail(c, response.ErrDatabase, "删除部门失败："+err.Error())
		return
	}

	response.Success(c, nil)
}

// QuerySysDeptVoList 获取部门下拉列表
func (d *DepartmentServiceImpl) QuerySysDeptVoList(c *gin.Context) {
	// 调用Repository层，获取模型数据
	depts, err := d.deptRepository.QuerySysDeptList()
	if err != nil {
		response.Fail(c, response.ErrDatabase, "获取部门列表失败："+err.Error())
		return
	}

	// 在服务层将模型转换为VO
	deptVoList := make([]vo.SysDeptVo, 0, len(depts))
	for _, dept := range depts {
		deptVo := vo.SysDeptVo{
			Id:       int(dept.ID),
			ParentId: int(dept.ParentId),
			Label:    dept.DeptName,
		}
		deptVoList = append(deptVoList, deptVo)
	}

	response.Success(c, deptVoList)
}
